package News.service;

import News.dao.BaseDao;
import News.dao.ClassRoomDao;
import News.util.News;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewsServer extends BaseDao implements ClassRoomDao {

    Connection conn=null;
    /**
     * 查询 语句
     * @return
     */
    @Override
    public Integer select(String sql,Object[] o) {
        Integer num=null;
        conn=getConn();
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            ps=conn.prepareStatement(sql);

            if(o!=null){
                for(int i=0;i<o.length;i++){
                    ps.setObject(i+1,o[i]);
                }
            }
            rs=ps.executeQuery();

            while(rs.next()){
                num=rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            clossAll(conn,ps,rs);
        }
        return num;
    }

    public List<News> selectInfo(String sql) {

        conn=getConn();
        PreparedStatement ps=null;
        ResultSet rs=null;
        News cr=null;
        List<News> list=new ArrayList<News>();
        try{
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while(rs.next()){
                cr=new News();
                cr.setNewId(rs.getString(1));
                cr.setNewInfo(rs.getString(2));
                list.add(cr);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            clossAll(conn,ps,rs);
        }
        return list;
    }

    public List<Map<String,Object>> query(String sql) {

        conn=getConn();
        PreparedStatement ps=null;
        ResultSet rs=null;
        News cr=null;
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        try{
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while(rs.next()){
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("id",rs.getString(1));
                map.put("text",rs.getString(2));
                list.add(map);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            clossAll(conn,ps,rs);
        }
        return list;
    }

    public int update(String sql, Object[] o) {
        int num=0;
        PreparedStatement ps=null;
        conn=getConn();
        try{
            ps=conn.prepareStatement(sql);
            if(ps!=null){
                for(int i=0;i<o.length;i++){
                    ps.setObject(i+1,o[i]);
                }
            }
            num=ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            clossAll(conn,ps,null);
        }
        return num;
    }

    public Map<String,Object> queryNews(String PageSize, String PageNo){
        if(PageSize==null){
            PageSize="5";
        }
        if(PageNo==null){
            PageNo="1";
        }

        Integer PageSizeInt=Integer.parseInt(PageSize);
        Integer PageNoInt=Integer.parseInt(PageNo);

        int start=PageSizeInt*(PageNoInt-1);
        int limit=PageSizeInt;

        Map<String,Object> resultMap=new HashMap<String,Object>();
        List<Map<String,Object>>queryList=new ArrayList<Map<String,Object>>();

        try{
            String selectNews="select newId,newInfo from WSQ.news where isdelete=1 limit "+start+","+limit;
            queryList = query(selectNews);
            Integer total=select("select count(1) from WSQ.news where isdelete=1",null);

            Integer totalPage=0;
            totalPage=total/PageSizeInt;
            if(total%PageSizeInt!=0){
                totalPage++;
            }
            Integer prevPage=1;
            if(PageNoInt>1){
                prevPage=PageNoInt-1;
            }
            Integer nextPage=1;
            if((totalPage-PageNoInt)>0){
                nextPage=PageNoInt+1;
            }else{
                nextPage=PageNoInt;
            }
            resultMap.put("list", queryList);
            resultMap.put("code", 200);
            resultMap.put("msg", "ok");
            resultMap.put("total",total);
            resultMap.put("totalPage",totalPage);
            resultMap.put("prevPage",prevPage);
            resultMap.put("nextPage",nextPage);
            resultMap.put("PageSize",PageSize);
            resultMap.put("PageNo",PageNo);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return resultMap;
    }

    public Integer queryCount(int id){
        Integer i=id*2;
        return i;
    }

    public List<Map<String,Object>> queryInfo(int id){
        List<Map<String,Object>> l=new ArrayList<>();
        Map<String,Object>m1=new HashMap<>();
        Map<String,Object>m2=new HashMap<>();
        m1.put("1",id);
        m1.put("2",id*2);
        m2.put("1",id*3);
        m2.put("2",id*4);
        l.add(m1);
        l.add(m2);
        return l;
    }

    public List<Map<String,Integer>> queryInfo(Map<String,Integer> id){
        List<Map<String,Integer>> l=new ArrayList<Map<String,Integer>>();
        l.add(id);
        return l;
    }

    public Map<String,Object> queryNewsInfo(String sql,int id){
        Map<String,Object> map=new HashMap<>();
        map.put("sql",sql);
        map.put("id",id);
        return map;
    }

    public Object[] queryNewsArrayInfo(String sql,int id){
        Object[] o={sql,id};
        return o;
    }

    public static void main(String[] args) {

    }

}
